AJAX_SUCCESS = false;
var httpRequest = function( options ){
    var default_options = {
        type : "GET",
        host : "",
        port : 80,
        uri  : "/",
        useragent : "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US)",
        headers : "",
        getdata : "",
        postdata : "",
        resolveTimeout : 10000,
        connectTimeout : 10000,
        sendTimeout : 10000,
        receiveTimeout : 10000
    }
    
    for( var key in options ){
        if( options.hasOwnProperty(key) && ( key in default_options ) ){
            default_options[key] = options[key];
        }
    }

    return  document.getElementById("simplehttpobj").http(   default_options.type
                                                             ,default_options.host
                                                             ,default_options.port
                                                             ,default_options.uri
                                                             ,default_options.useragent
                                                             ,default_options.headers
                                                             ,default_options.getdata
                                                             ,default_options.postdata
                                                             ,default_options.resolveTimeout
                                                             ,default_options.connectTimeout
                                                             ,default_options.sendTimeout
                                                             ,default_options.receiveTimeout
                                                         );

}

var ajax = function( url,handler ){
	var xhr = new XMLHttpRequest();
	xhr.onreadystatechange = handler; 
	xhr.open("GET", url, true);
	xhr.send(null);	
    ajax.abort = function(){xhr.abort();};
};

chrome.extension.onRequest.addListener(function( request,sender,sendResponse ){
	if( request.action != "geturl" || !request.urloption ){
		sendResponse({code:-1,message:"无效请求",data:null});
    }else{
        var response;
        var localHttpFailed = false;
        try{
            console.log( "local request -> " + "http://"+request.urloption.host+request.urloption.uri+request.urloption.getdata )
            response = httpRequest( request.urloption );
            console.log( response );
        }catch( err ){//dll出错
            localHttpFailed =  true;
        }

        if(/^\d+$/.test(response)){
            localHttpFailed = true;
        }
        
        if(!localHttpFailed){//本地加载数据成功
                try{
                     var jsonData = JSON.parse(response);
                         sendResponse({code:0,message:"成功",data:jsonData});
                }catch( err ){
                         sendResponse({code:-3,message:"数据解析失败",data:_this.status});
                }
        }else{//从网络加载

            var proxy = "http://hellohtml5.com/simple",
                url = "http://"+request.urloption.host+request.urloption.uri+request.urloption.getdata;
                request_url = proxy + "/" + url;        
                
                console.log("ajax->"+request_url);
    
              	ajax(request_url,function(){
    	    		var _this = this;			
                    if( _this.readyState  == 4 && _this.status==200 ){

                        AJAX_SUCCESS = true;

                        var responseText = _this.responseText.replace(/^[^\{]+/,'').replace(/[^\}]+$/,'');
                        console.log(responseText);
                        try{
                            var jsonData = JSON.parse(responseText);
                            sendResponse({code:0,message:"成功",data:jsonData});
                        }catch( err ){
                            sendResponse({code:-3,message:"数据解析失败",data:_this.status});
                        }
                    }else if(  _this.readyState  == 4 && _this.status != 200 ){
                        AJAX_SUCCESS = true;
    	    			sendResponse({code:-2,message:"http状态错误",data:_this.status});		
    	    		}
    		    });
    
                setTimeout(function(){//20秒后发送超时消息
                    if(!AJAX_SUCCESS){
                        ajax.abort();
                        sendResponse({code:-5,message:"超时，请重试"});	
                    }
                },20*1000);
        }

	}
});
